package com.eascs.spider.dsk.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.eascs.spider.dsk.exception.ConfigLoadException;

public class PropertiesUtil {
	private static Map<String, String> config = new HashMap<>();
	
	static{
		try {
			Properties prps = new Properties();
			File dir = null;
			if(System.getProperty("os.name").contains("Win")){
				dir = new File("src/main/resources");//win系统
			}else{
				dir = new File("./");//linux系统，配置文件跟jar文件处于同级目录下
			}
			
			for(File file : dir.listFiles()){
				if(file.getName().contains("properties")){
					prps.load(new FileInputStream(file));
				}
			}
			for(Object o : prps.keySet()){
				String key = String.valueOf(o);
				config.put(key, prps.getProperty(key));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 读取配置，根据key取值
	 * @param key
	 * @return
	 * @throws ConfigLoadException
	 */
	public static String getConfig(String key) throws ConfigLoadException{
		if(config.get(key) == null)
			throw new ConfigLoadException("配置读取异常 , key:" + key);
		return config.get(key);
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(config.toString());
	}
}
